package words

import (
	"context"
	"star/api/words/v1"
	"star/internal/logic/users"
	"star/internal/logic/words"
	"star/internal/model"
)

func (c *ControllerV1) List(ctx context.Context, req *v1.ListReq) (res *v1.ListRes, err error) {
	Uid, err := users.GetUid(ctx)
	if err != nil {
		return nil, err
	}
	query := &model.WordQuery{
		Uid:  Uid,
		Word: req.Word,
		Page: req.Page,
		Size: req.Size,
	}
	Wordlist, total, err := words.GetWordList(ctx, query)
	var list []model.WordList
	for _, v := range Wordlist {
		list = append(list, model.WordList{
			Id:                 v.Id,
			Word:               v.Word,
			Definition:         v.Definition,
			ProficiencyLevel:   v.ProficiencyLevel,
			ExampleSentence:    v.ExampleSentence,
			ChineseTranslation: v.ChineseTranslation,
			CreatedAt:          v.CreatedAt,
			UpdatedAt:          v.UpdatedAt,
		})
	}

	if err != nil {
		return nil, err
	}

	return &v1.ListRes{
		List:  list,
		Total: total,
	}, nil
}
